<?php
/**
 * Created by PhpStorm.
 * User: fengdh
 * Date: 2019/9/4
 * Time: 9:36
 */

namespace business\elasticsearch\models;

class ProcurementModel extends \yii\elasticsearch\ActiveRecord
{
    public function attributes()
    {
        return [
            'sjId', //主键id
            'sjType',   //商机类型
            'sjState',  //商机状态
            'sjTitle',  //商机标题
            'sjShortTitle', //短标题
            'sjTitlePinyin',    //标题拼音
            'sjBasicCategoryCode1', //商机分类code
            'sjBasicCategoryCode2', //
            'sjBasicCategoryCode3', //
            'sjBasicCategoryName1', //商机分类名
            'sjBasicCategoryName2', //
            'sjBasicCategoryName3', //
            'sjSearchBasicCategoryName',    //商机搜索基础分类
            'sjDueDate',    //商机报名截止日期
            'sjDueDateTimestamp',    //商机报名截止日期—时间戳
            'sjPublishDate',    //商机发布日期
            'sjPublishDateTimestamp',    //商机发布日期—时间戳
            'sjIsExpired',  //商机是否已截止
            'sjProjectLocationCode',    //商机项目所在地code
            'sjProjectLocationName',    //
            'sjSearchProjectLocationCode',  //
            'sjRegisterFund',   //商机报名注册资本要求
            'sjCaseNum',    //商机报名案例数量要求
            'sjQualificationCode',  //商机报名服务资质要求
            'sjQualificationName',  //
            'sjQualification_*',    //
            'sjServiceAreaCode',    //商机报名服务区域要求
            'sjServiceAreaName',    //
            'sjSupplierType',   //商机报名供应商类型要求
            'sjRegisterCondition',  //商机报名所有条件要求
            'sjMerit',  //商机优势
            'sjCoveredArea',    //商机建筑面积
            'sjDeveloperId',    //开发商id
            'sjDeveloperLocationCode',  //开发商所在地
            'sjDeveloperName', //开发商名称相关字段
            'sjDeveloperShortName',
            'sjSearchDeveloperName',
            'sjDeveloperLogo',  //开发商logo
            'sjDeveloperHonor', //开发商荣誉
            'isFirstPublishSj', //是否5天内首次发布招募
            'sjDeveloperLevel', //开发商级别
            'handleStorageCount',   //7天报名处理总数
            'sjDeveloperType',  //开发商类型(普通,百强,500强)
            'sjFilterLocationCode', //排序地区过滤字段
            'sjItemType', //招募类型
            'indexCreatedTime', //索引创建时间
            'sjSource',
            'tenantId', //开发商租户id
            'shortDetail', //招标简介,
            'enterpriseNature', // 企业类型
        ];
    }

    public static function index()
    {
        return 'procurement';
    }

    public static function type()
    {
        return 'my_type';
    }

    public static function getSjType($type)
    {
        return ['term' => ['sjType' => $type]];
    }

    public static function getSjDue($date, $state)
    {
        $range = [];
        $state == 0 && $range['from'] =  $date;
        $state == 1 && $range['to'] = $date;
        return ['range' => ['sjDueDate' => $range]];
    }

    public static function getSjState($state)
    {
        return ['term' => ['sjState' => $state]];
    }

    public static function getSupplierType($supplier_type)
    {
        return ['term' => ['sjSupplierType' => $supplier_type]];
    }

    public static function getDeveloperType($developer_type)
    {
        $developer_type = is_array($developer_type) ? $developer_type : [$developer_type];
        return ["terms" => ["sjDeveloperType.keyword" => $developer_type]];
    }

    public static function sjIsExpired($is_expired)
    {
        return ['term' => ['sjIsExpired' => $is_expired]];
    }

    public static function getLocationCode($code)
    {
        if (is_array($code)){
            $codes = $code;
        } else{
            $codes = explode(',', $code);
        }
        return ["terms" => ["sjFilterLocationCode" => $codes]];
    }

    public static function getServiceAreaCode($code)
    {
        $code = is_array($code) ? $code : [$code];
        return ["terms" => ["sjServiceAreaCode" => $code]];
    }

    public static function getServiceAreaName($name)
    {
        $name = is_array($name) ? $name : [$name];
        return ["terms" => ["sjServiceAreaName" => $name]];
    }

    public static function getRegisterFund($min = 0, $max = null)
    {
        $range = [];
        !empty($min) && $range['from'] =  $min;
        !empty($max) && $range['to'] = $max;
        return ['range' => ['sjRegisterFund' => $range]];
    }

    public static function getCaseNum($min = 0, $max = null)
    {
        $range = [];
        !empty($min) && $range['from'] =  $min;
        !empty($max) && $range['to'] = $max;
        return ['range' => ['sjCaseNum' => $range]];
    }

    public static function basicCategoryCode($code, $level = 1)
    {
        if (is_array($code)) {
            return ['terms' => ['sjBasicCategoryCode'.$level => $code]];
        } else {
            return ['term' => ['sjBasicCategoryCode'.$level => $code]];
        }
    }

}